package com.interfaces.module.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.interfaces.module.article.service.ArticleService;
import com.interfaces.module.book.domain.FtsBook;
import com.interfaces.module.book.domain.FtsBookCatalogue;
import com.interfaces.module.book.service.FtsBookCatalogueService;
import com.interfaces.module.book.service.FtsBookService;
import com.interfaces.module.common.controller.CommonController;
import com.interfaces.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/book")
public class FtsBookController extends CommonController<FtsBook> {
    @Autowired
    private FtsBookService ftsBookService;
    @Autowired
    private FtsBookCatalogueService ftsBookCatalogueService;
    @Autowired
    private ArticleService articleService;
    @Override
    protected IService<FtsBook> getCoreService() {
        return this.ftsBookService;
    }
    @GetMapping("/getById/{id}")
    @ResponseBody
    public Result getDataById(@PathVariable Serializable id) {
        FtsBook ftsBook=ftsBookService.getById(id);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("book_id",id);
        queryWrapper.orderByDesc("rank");
        List<FtsBookCatalogue> ftsBookCatalogueList=ftsBookCatalogueService.list(queryWrapper);
        ftsBookCatalogueList.stream().forEach(item->{
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper.eq("catalogue_id",item.getId());
            queryWrapper1.orderByDesc("rank");
            item.setArticleList(articleService.list(queryWrapper));
        });
        ftsBook.setBookCatalogueList(ftsBookCatalogueList);
        return Result.SUCCESS(ftsBook);
    }
}
